package com.wu.wechat.handel;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.nacos.shaded.com.google.gson.GsonBuilder;
import com.wu.common.exception.WexinReqException;
import com.wu.common.handel.WeiXinReqHandler;
import com.wu.entity.weixin.annotation.ReqType;
import com.wu.entity.weixin.po.MenuCreate;
import com.wu.entity.weixin.po.WeixinButton;
import com.wu.entity.weixin.po.WeixinReqConfig;
import com.wu.entity.weixin.requ.WeixinReqParam;
import com.wu.wechat.util.HttpRequestProxy;
import com.wu.common.utils.WeiXinReqUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class WeixinReqMenuCreateHandler implements WeiXinReqHandler {
    private static Logger logger = LoggerFactory.getLogger(WeixinReqMenuCreateHandler.class);

    public WeixinReqMenuCreateHandler() {
    }

    public String doRequest(WeixinReqParam weixinReqParam) throws WexinReqException {
        String strReturnInfo = "";
        if (weixinReqParam.getClass().isAnnotationPresent(ReqType.class)) {
            ReqType reqType = (ReqType)weixinReqParam.getClass().getAnnotation(ReqType.class);
            WeixinReqConfig objConfig = WeiXinReqUtil.getWeixinReqConfig(reqType.value());
            if (objConfig != null) {
                String reqUrl = objConfig.getUrl();
                MenuCreate mc = (MenuCreate)weixinReqParam;
                Map<String, String> parameters = new HashMap();
                parameters.put("access_token", mc.getAccess_token());
                String jsonData = "{" + this.getMenuButtonJson("button", mc.getButton()) + "}";
                logger.info("处理创建菜单" + jsonData);
                strReturnInfo = HttpRequestProxy.doJsonPost(reqUrl, parameters, jsonData);
            }
        } else {
            logger.info("没有找到对应的配置信息");
        }

        return strReturnInfo;
    }

    private String getMenuButtonJson(String name, List<WeixinButton> b) {
        StringBuffer json = new StringBuffer();
        json.append("\"" + name + "\":[");
        if (b != null && b.size() != 0) {
            List<WeixinButton> sub_button = null;
            Gson gson = (new GsonBuilder()).setPrettyPrinting().disableHtmlEscaping().create();
            String objJson = "";
            Iterator var7 = b.iterator();

            while(var7.hasNext()) {
                WeixinButton m = (WeixinButton)var7.next();
                sub_button = m.getSub_button();
                m.setSub_button((List)null);
                objJson = gson.toJson(m);
                json.append(objJson);
                if (sub_button != null && sub_button.size() > 0) {
                    json.setLength(json.length() - 1);
                    json.append(",");
                    objJson = this.getMenuButtonJson("sub_button", sub_button);
                    json.append(objJson);
                    json.append("}");
                }

                m.setSub_button(sub_button);
                json.append(",");
            }

            json.setLength(json.length() - 1);
            json.append("]");
            return json.toString();
        } else {
            return json.append("]").toString();
        }
    }
}
